package cn.snowtech.bluebox.controller;

import cn.snowtech.bluebox.domain.Result;
import cn.snowtech.bluebox.dto.medicine.*;
import cn.snowtech.bluebox.service.MedicineService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 提醒事项控制器.
 */
@RestController
public class MedicineController {
    private final static Logger loger= LoggerFactory.getLogger(MedicineController.class);

    @Autowired
    MedicineService medicineService;


    @RequestMapping(value = "createMedicine",consumes = "application/json",method = RequestMethod.POST)
    private MedicineResponseDTO createReminder(@RequestBody String medicine){
        ObjectMapper mapper=new ObjectMapper();
        MedicineRequestDTO requestDTO=null;
        MedicineResponseDTO responseDTO=new MedicineResponseDTO();

        try {
            requestDTO=mapper.readValue(medicine,MedicineRequestDTO.class);
        } catch (IOException e) {
            if (loger.isDebugEnabled()){
                loger.debug(e.getMessage());
            }
            responseDTO.setResult(Result.R1002.getCode());
            responseDTO.setMessage(Result.R1002.getMessage());
            return responseDTO;
        }
        return medicineService.create(requestDTO);
    }

    @RequestMapping(value = "getAllMedicines",consumes = "application/json",method = RequestMethod.POST)
    private MedicineQueryResponseDTO getAllMedicines(@RequestBody String catalog){
        ObjectMapper mapper=new ObjectMapper();
        MedicineQueryRequestDTO requestDTO=null;
        MedicineQueryResponseDTO responseDTO=new MedicineQueryResponseDTO();

        try {
            requestDTO=mapper.readValue(catalog,MedicineQueryRequestDTO.class);
        } catch (IOException e) {
            if (loger.isDebugEnabled()){
                loger.debug(e.getMessage());
            }
            responseDTO.setResult(Result.R1002.getCode());
            responseDTO.setMessage(Result.R1002.getMessage());
            return responseDTO;
        }
        return medicineService.getMedicines(requestDTO);
    }

    @RequestMapping(value = "searchMedicine",consumes = "application/json",method = RequestMethod.POST)
    private MedicineQueryResponseDTO searchMedicine(@RequestBody String catalog){
        ObjectMapper mapper=new ObjectMapper();
        MedicineSearchRequestDTO requestDTO=null;
        MedicineQueryResponseDTO responseDTO=new MedicineQueryResponseDTO();

        try {
            requestDTO=mapper.readValue(catalog,MedicineSearchRequestDTO.class);
        } catch (IOException e) {
            if (loger.isDebugEnabled()){
                loger.debug(e.getMessage());
            }
            responseDTO.setResult(Result.R1002.getCode());
            responseDTO.setMessage(Result.R1002.getMessage());
            return responseDTO;
        }
        return medicineService.searchMedicines(requestDTO);
    }
}
